package com.ilancuo.cnbeta;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 生成签名的工具类
 */
public class SignUtil {

    //    private static final char[] a = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70};
    //    private static final char[] a = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private static final char[] a = "0123456789ABCDEF".toCharArray();

    private static String a(byte[] param) {
        StringBuilder sb = new StringBuilder(param.length * 2);
        for (byte p : param)
            sb.append(a[((p & 0xF0) >>> 4)]).append(a[(p & 0xF)]);
        return sb.toString();
    }

    public static String getSign(String param) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(param.getBytes());
            param = a(digest.digest()).toLowerCase();
            return param;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
